package dtu.app.activity;

import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
import com.iqengines.demo.R;

import dtu.app.fragment.TempProductListFragment;
import dtu.app.fragment.TempProductListFragment.OnProductTempItemSelectedListener;

public class TempProductActivity extends FragmentActivity implements OnProductTempItemSelectedListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.products_temp);
		setTitle("Temporary Products");
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setHomeButtonEnabled(true);
		
		// to give support on lower android version, we are not calling getFragmentManager() 
		FragmentManager fm = getSupportFragmentManager();

		if (fm.findFragmentById(R.id.productTempList) == null) {
			TempProductListFragment list = new TempProductListFragment();
			fm.beginTransaction().add(R.id.productTempList, list).commit();
		}
	}
	
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home:
			// app icon in action bar clicked; go home
			finish();
			return true;
		case R.id.menu_add_items:
			addItemsToStock();
			
			Intent intent = new Intent(this, BaseActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		case R.id.menu_scan: startActivity(new Intent(this, ScanActivity.class)); return true;
		//		case R.id.menu_tutorial: startActivity(new Intent(this, TutorialActivity.class)); break;
		//		case R.id.menu_settings: startActivity(new Intent(this, DemoActivity.class)); break;
		default:
			return super.onMenuItemSelected(featureId, item);
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		//		TempProductListFragment list = (TempProductListFragment) getSupportFragmentManager().findFragmentById(R.id.productTempList);
		//		list.saveListChanges();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.temp_prod_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public void onProductTempItemSelected(String category) {
		// TODO Auto-generated method stub

	}

	private void addItemsToStock(){
		TempProductListFragment list = (TempProductListFragment) getSupportFragmentManager().findFragmentById(R.id.productTempList);
		list.addItemsToStock();
	}

}